import React, {Component} from 'react';
import './index.less';
import {promptBox, toast} from 'nc-lightapp-front';
import ReferSearch from '../../../../../../hrpub/common/components/referSearch/org';
import setQueryMeta from '../../container/functions/setQueryMeta';
import updateButtonStatus from '../../container/functions/updateButtonStatus';
import deleteOpinionData from '../../container/functions/deleteOpinionData';
import renewalContract from '../../container/functions/renewalContract';
import stopContract from '../../container/functions/stopContract';
import checkOpinionMsg from '../../container/functions/checkOpinionMsg';
import sendOpinionMsg from '../../container/functions/sendOpinionMsg';
import AlertModal from '../AlertModal';
import do_print from 'src/hrpub/common/utils/print';
import NCBackBtn from 'src/hrpub/common/components/hr-back';

class Header extends Component {
    constructor(props) {
        super(props);
        this.state = {printData: {}};
        this.updateOrgValue = this.updateOrgValue.bind(this);
        this.onBtnOperation = this.onBtnOperation.bind(this);
        this.back = this.back.bind(this);
    }

    updateOrgValue(value) {
        this.props.updateOrgValue(value);
    }

    back() {
        const {json, updateState, editTable, orgVal} = this.props;
        promptBox({
            color: 'warning',
            title: json['cm6011-000000'], // 确认删除/* 国际化处理： 确认取消,确认取消*/
            content: json['cm6011-000031'], // 是否删除这个报表，/* 国际化处理： 是否确认要取消？*/
            beSureBtnClick: () => {
                updateState({
                    isEdit: false
                }, () => {
                    editTable.cancelEdit('contopiniongrid');
                    updateButtonStatus.call(this, orgVal);
                    this.hideAction();
                });
            }
        });
    }

    onBtnOperation(props, btncode, areacode, opt) {
        const {json, updateState, editTable, orgVal, getMainTableData, form, showOpinionAsk} = this.props;
        switch (btncode) {
            case 'add':
                showOpinionAsk();
                break;
            case 'edit':
                updateState({
                    isEdit: true
                }, () => {
                    this.addAction();
                    editTable.setStatus('contopiniongrid', 'edit');
                    const rows = editTable.getAllRows('contopiniongrid');
                    rows.forEach((row) => {
                        if (row.values.conttype.value !== '0') {
                            editTable.setEditableRowByRowId('contopiniongrid', row.rowid, false);
                            if (row.values.conttype.value === '3') {
                                editTable.setEditableRowKeyByRowId('contopiniongrid', row.rowid, 'hropinion', true);
                            }
                        }
                    });
                    updateButtonStatus.call(this, orgVal, true);
                });
                break;
            case 'delete':
                deleteOpinionData.call(this, () => {
                    getMainTableData();
                });
                break;
            case 'sendMsg':
                checkOpinionMsg.call(this, () => {
                    sendOpinionMsg.call(this, () => {
                        getMainTableData();
                    })
                });
                break;
            case 'renewal':
                renewalContract.call(this, () => {
                    getMainTableData();
                });
                break;
            case 'stop':
                stopContract.call(this, (data) => {
                    updateState(
                        {
                            stopModalVisible: true,
                            stopPkOpinions: data.pk_opinions
                        },
                        () => {
                            data.data.ctrtoperatedlg.rows[0].values['pk_psndoc.code'].display = null;
                            data.data.ctrtoperatedlg.rows[0].values['pk_psndoc.name'].display = null;
                            form.setAllFormValue({ctrtoperatedlg: data.data.ctrtoperatedlg});
                        }
                    );
                });
                break;
            case 'save':
                this.props.saveOpinion(this.hideAction);
                break;
            case 'cancel':
                this.back();
                break;
            case 'query':
                setQueryMeta.call(this);
                this.props.search.openAdvSearch('search', true);
                break;
            case 'refresh':
                getMainTableData(true);
                break;
            case 'print':
                const el = document.querySelector('.main-table').cloneNode(true);
                const table = el.querySelector('.u-table-content');
                table.removeChild(table.lastElementChild);
                const tHeader = el.querySelector('.u-table-header');
                const tr = tHeader.querySelector('tr');
                tr.removeChild(tr.firstElementChild);
                //tr.removeChild(tr.firstElementChild);
                do_print(
                    el,
                    {
                        title: json['cm6011-000032'] /* 国际化处理： 续签意见征询*/,
                        maker: json['cm6011-000033'] /* 国际化处理： 打印人*/,
                        date: json['cm6011-000034'] /* 国际化处理： 打印日期*/
                    },
                    {
                        tableTmp: this.props.meta.getMeta()['contopiniongrid'],
                        data: editTable.getAllRows('contopiniongrid')
                    }
                );
                break;
            default:
                break;
        }
    }

    hideAction = () => {
        const meta = this.props.meta.getMeta();
        const index = meta['contopiniongrid'].items.findIndex(item => item.attrcode === 'opr');
        if (index > -1) {
            meta['contopiniongrid'].items.splice(index, 1);
        }
        this.props.meta.setMeta(meta);
    };

    addAction = () => {
        const {json} = this.props;
        const meta = this.props.meta.getMeta();
        const index = meta['contopiniongrid'].items.findIndex(item => item.attrcode === 'opr');
        if (index === -1) {
            meta['contopiniongrid'].items.push({
                itemtype: 'customer',
                width: '80px',
                label: json['cm6011-000064'],
                visible: true,
                fixed: 'right',
                attrcode: 'opr',
                render: (text, record, index) => {
                    return (record.values.conttype.value !== '0' && record.values.conttype.value !== '1' ? null :
                        <a
                            href="javascript:void(0)"
                            className="operator-btn"
                            onClick={
                                () => {
                                    this.deleteLine(record, index);
                                }
                            }
                        >
                            {json['cm6011-000065']}
                        </a>);
                }
            });
        }
        this.props.meta.setMeta(meta);
    };

    deleteLine = (record, index) => {
        const {editTable, json} = this.props;
        if (record.values.conttype.value !== '0' && record.values.conttype.value !== '1') {
            toast({color: 'warning', content: json['cm6011-000040']}); /* 国际化处理： 只能删除状态为“未征询”和“未反馈”的征询记录!*/
            return;
        }
        editTable.deleteTableRowsByIndex('contopiniongrid', index);
    };

    render() {
        const {button, orgVal, isEdit, json} = this.props;
        return (
            <div>
                <div className="header">
                    {!isEdit ? <div className="header-refer">
                        <ReferSearch orgVal={orgVal} getOrgData={this.updateOrgValue}/>
                    </div> : <NCBackBtn onClick={this.back}/>}
                    <div className="btn-group">
                        <div className="definition-icons">
                            {button.createButtonApp({
                                area: 'head',
                                onButtonClick: (props, btncode) => {
                                    this.onBtnOperation(props, btncode, 'head', this);
                                }
                            })}
                        </div>
                    </div>
                </div>
                <AlertModal ref={(node) => (this.alertModalNode = node)} json={json}/>
            </div>
        );
    }
}

export default Header;
